package com.bubble;

/**
 * Created by HMF on 2021/08/01 22:33
 */

/**
 * 冒泡排序
 */
public class Solution {

    // 冒泡排序
    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) { // 相邻的两个元素比对
                    int temp = arr[j]; // 两两比较
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    // main函数
    public static void main(String[] args) {
        int[] array = {7, 3, 19, 40, 4, 7, 1};
//        int[] array = {38, 65, 97, 76, 13, 27, 49};
        bubbleSort(array);
        // 打印显示排序后的数组
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }
}
